package com.github_dididipapa.mall.ware.service.impl;

import com.github_dididipapa.common.exception.OpenFeignException;
import com.github_dididipapa.common.to.SkuInfoTo;
import com.github_dididipapa.mall.ware.feign.ProductFeignService;
import com.github_dididipapa.mall.ware.vo.SkuHasStockVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github_dididipapa.common.utils.PageUtils;
import com.github_dididipapa.common.utils.Query;

import com.github_dididipapa.mall.ware.dao.WareSkuDao;
import com.github_dididipapa.mall.ware.entity.WareSkuEntity;
import com.github_dididipapa.mall.ware.service.WareSkuService;
import org.springframework.util.StringUtils;

@Slf4j
@RequiredArgsConstructor
@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    private final WareSkuDao wareSkuDao;
    private final ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String wareId = (String) params.get("wareId");
        String skuId = (String) params.get("skuId");


        if(StringUtils.hasText(wareId)) wrapper.eq("ware_id",wareId);
        if(StringUtils.hasText(skuId)) wrapper.eq("sku_id",skuId);

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1.判断如果还没有这个库存记录，则新增
        List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>()
                .eq("sku_id", skuId).eq("ware_id", wareId));
        if(wareSkuEntities == null || wareSkuEntities.isEmpty()){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //远程查询sku的名字
            SkuInfoTo skuInfo  = productFeignService.getSkuInfo(skuId);
            if (skuInfo == null) {
                log.info("{}:远程调用查询sku名字失败",this.getClass());
                throw new OpenFeignException("远程调用查询sku名字失败");
            }
            else {
                wareSkuEntity.setSkuName(skuInfo.getSkuName());
                wareSkuDao.insert(wareSkuEntity);
            }
        }
        //2.有库存记录，则进行更新
        else{
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkusHasStock(List<Long> skuIds) {
        List<WareSkuEntity> skus = wareSkuDao.selectList(
                new QueryWrapper<WareSkuEntity>()
                        .in("sku_id", skuIds));

        return skus.stream().map(sku -> {
            SkuHasStockVo vo = new SkuHasStockVo();
            vo.setSkuId(sku.getSkuId());
            vo.setHasStock((sku.getStock() - sku.getStockLocked()) > 0);
            return vo;
        }).collect(Collectors.toList());
    }

}